﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
# if XBOX || WINDOWS
using Microsoft.Xna.Framework.Storage;
#endif
using Microsoft.Xna.Framework.GamerServices;

namespace The_LarX
{
    public class StorageDeviceManager
    {
        protected bool DeviceLoaded;

        protected bool UserChosenToNotUseStorage;

        protected bool inActivity;
        public bool InActivity
        {
            get { return inActivity; }
        }

        protected IAsyncResult Result;
#if XBOX || WINDOWS
        protected StorageDevice device;
        public StorageDevice Device
        {
            get 
            {
                if (device == null || !device.IsConnected)
                {
                    if (!UserChosenToNotUseStorage)
                    {
                        DeviceLoaded = false;
                        inActivity = true;
                    }
                }
                return device; 
            }
        }                

        public StorageDeviceManager()
        {
        }

#endif

        public virtual void DeactivateDevice()
        {
#if XBOX || WINDOWS
            device = null;
            DeviceLoaded = false;
            UserChosenToNotUseStorage = false;
            inActivity = true;
            Result = null;
#endif
        }
     
        public virtual void Update()
        {
#if XBOX || WINDOWS
            if (!UserChosenToNotUseStorage)
            {
                if (device == null || !device.IsConnected)
                {
                    DeviceLoaded = false;
                    inActivity = true;
                    Result = null;
                }
            }

            if (!DeviceLoaded)
            {                
                try
                {
                    if (Result == null && !Guide.IsVisible)
                    {
                        Result = StorageDevice.BeginShowSelector(ASync, null);
                    }
                }
                catch
                {
                    while (Guide.IsVisible)
                    {
                        // If the Guide is opened, wait until close
                    }
                    if (Result == null && !Guide.IsVisible)
                    {
                        Result = StorageDevice.BeginShowSelector(ASync, null);
                    }
                }
            }
            while (!DeviceLoaded)
            {
                // Wait
            }
#endif
        }

        public void ASync(IAsyncResult call)
        {
#if XBOX || WINDOWS
            try
            {
                device = StorageDevice.EndShowSelector(call);
                DeviceLoaded = true;
                inActivity = false;
                if (device == null)
                {
                    UserChosenToNotUseStorage = true;
                }

            }
            catch
            {
                UserChosenToNotUseStorage = true;
                DeviceLoaded = true;
                inActivity = false;
            }
#endif
        }

#if XBOX || WINDOWS
        protected List<StorageContainer> Containers = new List<StorageContainer>();
        public StorageContainer GetStorageContainer(string displayName)
        {
            try
            {
                DisableAllContainers();

                StorageDevice Device = Engine.StorageDeviceManager.Device;
                IAsyncResult res = Device.BeginOpenContainer(displayName, null, null);

                while (!res.IsCompleted)
                {
                    // Wait until the Player chooses a StorageDevice
                }

                StorageContainer container = Device.EndOpenContainer(res);
                Containers.Add(container);
                return container;
            }
            catch
            {
            }

            return null;
        }
#endif
        public void DisableAllContainers()
        {
#if WINDOWS || XBOX
            for (int i = 0; i < Containers.Count; i++)
            {
                if (!Containers[i].IsDisposed)
                    Containers[i].Dispose();
            }
            Containers.Clear();
#endif
        }

    }
        
}
